package com.estronger.shopmanage.ui.order.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.estronger.shopmanage.R;
import com.estronger.shopmanage.base.BaseActivity;
import com.estronger.shopmanage.base.MyApplication;
import com.estronger.shopmanage.manager.NetManager;
import com.estronger.shopmanage.ui.order.until.Tools;
import com.estronger.shopmanage.ui.order.adapter.SelectHousesAdpter;
import com.estronger.shopmanage.ui.order.bean.OrderAttributeBean;
import com.estronger.shopmanage.utils.Toast;
import com.lidroid.xutils.http.RequestParams;

import java.util.ArrayList;

/**
 * Created by DingYG on 2017-07-26.
 */

public class SelectRegionActivity extends BaseActivity implements View.OnClickListener {

    private View rl_root;
    private Activity mContext;
    private TextView tvfCity, tvfCountry;
    private ImageView imgfCityLine, imgfCountyLine;
    private ListView lvfCity;
    private TextView tvfClickToCancle;
    private ArrayList<OrderAttributeBean> cityList;
    private ArrayList<OrderAttributeBean> countList;
    private int type = -1;


    @Override
    protected void init() {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        mContext = SelectRegionActivity.this;
        rl_root = View.inflate(mContext, R.layout.activity_select_region, null);
        setContentView(rl_root);
        setFinishOnTouchOutside(false);
        Bundle bundle = getIntent().getExtras();
        type = bundle.getInt("CITYORCOUNTRY");
        if(type==0){
            cityList = bundle.getParcelableArrayList("BUNDLE_CITY");
        }else if(type==1){
            countList = bundle.getParcelableArrayList("BUNDLE_COUNTRY");
        }
        //设置Theme.Dialog View高度
        WindowManager m = getWindowManager();
        Display d = m.getDefaultDisplay(); // 为获取屏幕宽、高
        android.view.WindowManager.LayoutParams p = getWindow().getAttributes();
        p.height = (int) (d.getHeight() * 0.5);
        p.width = (int) (d.getWidth()); // 宽度设置为屏幕的0.7
        p.gravity = Gravity.BOTTOM;
        getWindow().setAttributes(p);
    }

    @Override
    protected void initView() {
        tvfCity = (TextView) findViewById(R.id.tvfCity);
        tvfCountry = (TextView) findViewById(R.id.tvfCounty);
        imgfCityLine = (ImageView) findViewById(R.id.imgfCityLine);
        imgfCountyLine = (ImageView) findViewById(R.id.imgfCountyLine);
        lvfCity = (ListView) findViewById(R.id.lvfCity);
        tvfClickToCancle = (TextView) findViewById(R.id.tvfClickToCancle);
    }

    @Override
    protected void initAction() {
        tvfCity.setOnClickListener(this);
        tvfClickToCancle.setOnClickListener(this);
        showReginList(type);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tvfClickToCancle:
                finish();
                break;
            case R.id.tvfCity:
                getRegionData(1);
                break;
        }
    }

    /**
     * 加载数据
     */
    private void getRegionData(int type) {
        switch (type) {
            case 1://城市
                RequestParams params = new RequestParams();
                params.addBodyParameter("sf_action", "getcitys");
                params.addBodyParameter("usercode", MyApplication.getUserCode());
                connect(params, NetManager.ORDER_SELECT_CITY);
                break;
            case 2://区县
                RequestParams params3 = new RequestParams();
                params3.addBodyParameter("sf_action", "getdistricts");
                params3.addBodyParameter("usercode", MyApplication.getUserCode());
                params3.addBodyParameter("cityid", Tools.sCity.getAttId());
                connect(params3, NetManager.ORDER_SELECT_COUNTRY);
                break;
        }
    }
    /**
     * 展示列表
     */
    private void showReginList(int type) {
        switch (type) {
            case 0://城市
                setTabColor(1);
                final SelectHousesAdpter adpter = new SelectHousesAdpter(mContext, cityList);
                lvfCity.setAdapter(adpter);
                lvfCity.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        //获取到需要传回的数据后 ，绑定在跳转的intent上
                        Tools.sCity = adpter.getItem(position);
                        getRegionData(2);
                    }
                });
                break;
            case 1://区县
                setTabColor(2);
                final SelectHousesAdpter countAdpter = new SelectHousesAdpter(mContext,countList);
                lvfCity.setAdapter(countAdpter);
                lvfCity.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        //获取到需要传回的数据后 ，绑定在跳转的intent上
                        Tools.sCountry = countAdpter.getItem(position);
                        Intent intent = new Intent(mContext, OrderEntryActivity.class);
                        mContext.setResult(Activity.RESULT_OK, intent);//Result_OK 表示结果响应成功
                        mContext.finish();
                    }
                });
                break;

        }
    }

    @Override
    protected void onNetSuccess(int code, String result) {
        super.onNetSuccess(code, result);
        if (code == NetManager.ORDER_SELECT_COUNTRY) {
                setTabColor(2);
            ArrayList<OrderAttributeBean> countDatas = Tools.getResponseData(result, "districtid", "districtname");
            if (countDatas == null || countDatas.size() == 0) {
                Toast.show(mContext, "未获取到"+Tools.sCity.getAttName()+"的区县信息！",Toast.LENGTH_SHORT);
            } else {
                final SelectHousesAdpter adpter = new SelectHousesAdpter(mContext,countDatas);
                lvfCity.setAdapter(adpter);
                lvfCity.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        //获取到需要传回的数据后 ，绑定在跳转的intent上
                        Tools.sCountry = adpter.getItem(position);
                        Intent intent = new Intent(mContext, OrderEntryActivity.class);
                        mContext.setResult(Activity.RESULT_OK, intent);//Result_OK 表示结果响应成功
                        mContext.finish();
                    }
                });
            }
        }else if(code == NetManager.ORDER_SELECT_CITY){
            setTabColor(1);
            ArrayList<OrderAttributeBean> cityDatas = Tools.getResponseData(result, "cityid", "cityname");
            if (cityDatas == null || cityDatas.size() == 0) {
                Toast.show(mContext, "未获取到"+Tools.dShop.getProvincename()+"的城市信息！",Toast.LENGTH_SHORT);
            } else {
                final SelectHousesAdpter adpter = new SelectHousesAdpter(mContext,cityDatas);
                lvfCity.setAdapter(adpter);
                lvfCity.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        //获取到需要传回的数据后 ，绑定在跳转的intent上
                        Tools.sCity = adpter.getItem(position);
                        getRegionData(2);
                    }
                });
            }
        }
    }

    @Override
    protected void onNetErorr(int code, String msg) {
        super.onNetErorr(code, msg);
    }

    private void setTabColor(int type) {
        switch (type) {
            case 1:
                imgfCountyLine.setVisibility(View.INVISIBLE);
                imgfCityLine.setVisibility(View.VISIBLE);
                tvfCountry.setTextColor(getResources().getColor(R.color.unselect_color2));
                tvfCity.setTextColor(getResources().getColor(R.color.nametx));
                break;
            case 2:
                imgfCountyLine.setVisibility(View.VISIBLE);
                imgfCityLine.setVisibility(View.INVISIBLE);
                tvfCity.setTextColor(getResources().getColor(R.color.unselect_color2));
                tvfCountry.setTextColor(getResources().getColor(R.color.nametx));
        }
    }
}